package com.cskaoyan.service.back;


import com.cskaoyan.bean.bo.back.BrandCreateBO;
import com.cskaoyan.bean.bo.back.BrandUpdateBO;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.back.*;
import com.cskaoyan.config.MagicValue;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.mapper.MarketRegionMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 品牌制造商服务
 * @author: jzc
 * @since: 2022/10/12 10:33
 **/
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    MarketRegionMapper marketRegionMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;


    /**
     * 获取省、市、区全部数量
     *
     * @return {@link Integer}
     */
    @Override
    public Integer getRegionCount() {
        MarketRegionExample marketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = marketRegionExample.createCriteria();
        criteria.andTypeEqualTo(MagicValue.REGION_TYPE_PRIVICE);
        long regionCount = marketRegionMapper.countByExample(marketRegionExample);
        Integer regionCountToInteger = Math.toIntExact(regionCount);
        return regionCountToInteger;
    }

    /**
     * 获取省、市、区列表
     *
     * @return {@link List}
     */
    @Override
    public List getRegionList() {
        //获取所有省份列表
        MarketRegionExample marketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = marketRegionExample.createCriteria();
        criteria.andTypeEqualTo(MagicValue.REGION_TYPE_PRIVICE);
        List<MarketRegion> marketRegions = marketRegionMapper.selectByExample(marketRegionExample);
        //获取所有市列表
        MarketRegionExample marketRegionExample2 = new MarketRegionExample();
        MarketRegionExample.Criteria criteria2 = marketRegionExample2.createCriteria();
        criteria2.andTypeEqualTo(MagicValue.REGION_TYPE_CITY);
        List<MarketRegion> marketRegions2 = marketRegionMapper.selectByExample(marketRegionExample2);
        //获取所有县或区列表
        MarketRegionExample marketRegionExample3 = new MarketRegionExample();
        MarketRegionExample.Criteria criteria3 = marketRegionExample3.createCriteria();
        criteria3.andTypeEqualTo(MagicValue.REGION_TYPE_COUNTY);
        List<MarketRegion> marketRegions3 = marketRegionMapper.selectByExample(marketRegionExample3);

        // 创建一个返回所有列表
        ArrayList<RegionVO> regionListVOS = new ArrayList<>();
        //获取所有省份列表
        for (MarketRegion marketRegion : marketRegions) {
            RegionVO regionVO = new RegionVO(
                    marketRegion.getId(),
                    marketRegion.getName(),
                    Integer.valueOf(marketRegion.getType()),
                    marketRegion.getCode(),
                    new ArrayList<>()
            );
            regionListVOS.add(regionVO);
        }
        //获取省份对应的所有市列表
        for (MarketRegion marketRegion : marketRegions2) {
            // ArrayList<RegionVO2> regionVO2s = new ArrayList<>();
            Integer CityPid = marketRegion.getPid();
            for (RegionVO regionListVO : regionListVOS) {
                // ArrayList<RegionVO2> regionVO2s = new ArrayList<>();
                List<RegionVO2> regionVO2s = regionListVO.getChildren();
                if (CityPid.equals(regionListVO.getId())) {
                    RegionVO2 regionVO2 = new RegionVO2(
                            marketRegion.getId(),
                            marketRegion.getName(),
                            Integer.valueOf(marketRegion.getType()),
                            marketRegion.getCode(),
                            new ArrayList<>()
                    );
                    regionVO2s.add(regionVO2);
                }
                regionListVO.setChildren(regionVO2s);
            }
        }
        //获取市对应的所有县、区列表
        for (MarketRegion marketRegion : marketRegions3) {
            Integer CityPid = marketRegion.getPid();
            for (RegionVO regionListVO : regionListVOS) {
                List<RegionVO2> regionVO2s = regionListVO.getChildren();
                for (RegionVO2 regionVO2 : regionVO2s) {
                    List<RegionVO3> regionVO3s = regionVO2.getChildren();
                    if (CityPid.equals(regionVO2.getId())) {
                        RegionVO3 regionVO3 = new RegionVO3(
                                marketRegion.getId(),
                                marketRegion.getName(),
                                Integer.valueOf(marketRegion.getType()),
                                marketRegion.getCode());
                        regionVO3s.add(regionVO3);
                    }
                    regionVO2.setChildren(regionVO3s);
                }
            }
        }
        return regionListVOS;
    }

    /**
     * 获取品牌列表
     * 模糊查询
     *
     * @param pageInfo 请求参数
     * @param id       品牌商id
     * @param name     品牌商名字
     * @return {@link CommonData}
     */
    @Override
    public CommonData getbrandList(BasePageInfo pageInfo, Integer id, String name) {
        CommonData<MarketBrand> CommonData = new CommonData<>();
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getLimit());
        marketBrandExample.setOrderByClause(pageInfo.getSort() + " " + pageInfo.getOrder());
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (id != null && name != null) {
            criteria.andIdEqualTo(id);
            criteria.andNameLike("%" + name + "%");
        }
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>(marketBrands);
        long BrandCount = marketBrandMapper.countByExample(marketBrandExample);
        CommonData.setTotal((int) BrandCount);
        CommonData.setPages(marketBrandPageInfo.getPages());
        CommonData.setList(marketBrandPageInfo.getList());
        return CommonData;
    }

    /**
     * 创建品牌商
     *
     * @param brandCreateBO 品牌创建博
     * @return {@link BrandCreateVO}
     */
    @Override
    public BrandCreateVO Createbrand(BrandCreateBO brandCreateBO) {

        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        criteria.andNameEqualTo(brandCreateBO.getName());
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        MarketBrand marketBrand = new MarketBrand(
                null,
                brandCreateBO.getName(),
                brandCreateBO.getDesc(),
                brandCreateBO.getPicUrl(),
                null,
                brandCreateBO.getFloorPrice(),
                new Date(),
                new Date(),
                null
        );
        if (marketBrands.size() == 1) {
            int delete = marketBrandMapper.deleteByExample(marketBrandExample);
        }
        marketBrandMapper.insertSelective(marketBrand);
        MarketBrand marketBrand2 = marketBrandMapper.selectByPrimaryKey(marketBrand.getId());
        BrandCreateVO brandCreateVO = new BrandCreateVO(
                marketBrand2.getId(),
                marketBrand2.getName(),
                marketBrand2.getDesc(),
                marketBrand2.getPicUrl(),
                marketBrand2.getFloorPrice(),
                marketBrand2.getAddTime(),
                marketBrand2.getUpdateTime()
        );
        return brandCreateVO;
    }

    /**
     * 修改品牌商信息
     *
     * @param brandUpdateBO 品牌更新博
     * @return {@link BrandUpdateVO}
     */
    @Override
    public BrandUpdateVO Updatebrand(BrandUpdateBO brandUpdateBO) {
        Date updateTime = new Date();
        MarketBrand marketBrand = new MarketBrand(
                brandUpdateBO.getId(),
                brandUpdateBO.getName(),
                brandUpdateBO.getDesc(),
                brandUpdateBO.getPicUrl(),
                null,
                brandUpdateBO.getFloorPrice(),
                brandUpdateBO.getAddTime(),
                updateTime,
                null
        );
        int effected = marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
        BrandUpdateVO brandUpdateVO = new BrandUpdateVO(
                brandUpdateBO.getId(),
                brandUpdateBO.getName(),
                brandUpdateBO.getDesc(),
                brandUpdateBO.getPicUrl(),
                brandUpdateBO.getFloorPrice(),
                brandUpdateBO.getAddTime(),
                updateTime
        );
        return brandUpdateVO;
    }

    /**
     * 删除品牌商
     *
     * @param brandUpdateBO 品牌更新博
     * @return {@link Integer}
     */
    @Override
    public Integer Deletebrand(BrandUpdateBO brandUpdateBO) {
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setId(brandUpdateBO.getId());
        marketBrand.setDeleted(true);
        int effected = marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
        // int effected = marketBrandMapper.deleteByPrimaryKey();
        return effected;
    }
}
